﻿using System;
using System.Data;
using System.ServiceModel;
using System.ServiceModel.Activation;

using Silverlight.DataSetConnector;
using System.Data.SqlClient;
using System.Configuration;

    /// <summary>
    /// Manage "Persons" use case
    /// </summary>
    
    /// <summary>
    /// Application tier manager
    /// </summary>
    internal static class ApplicationTierManager
    {
        /// <summary>
        /// Prepares data for application initial load
        /// </summary>
        /// <param name="dataSet">DataSet instance</param>
        public static void InitialLoad(DataSet dataSet)
        {
            DataTable requestParametersDataTable = new DataTable("RequestParameters");

            DataColumn dataColumn = new DataColumn("Requestor", typeof(string));
            requestParametersDataTable.Columns.Add(dataColumn);

            dataColumn = new DataColumn("RequestMethhod", typeof(string));
            requestParametersDataTable.Columns.Add(dataColumn);

            DataRow dataRow = requestParametersDataTable.NewRow();

            // Assign startup page from web.config file
            dataRow["Requestor"] = ConfigurationManager.AppSettings["StartupPage"];
            requestParametersDataTable.Rows.Add(dataRow);

            // Call AcceptChanges method on requestParametersDataTable and add it to DataSet
            requestParametersDataTable.AcceptChanges();
            dataSet.Tables.Add(requestParametersDataTable);
        }

        /// <summary>
        /// Processes request from Silverlight front-end
        /// </summary>
        /// <param name="dataSet">DataSet instance</param>
        public static void ProcessRequest(DataSet dataSet)
        {
            //// Check whether it is initial request when application is just loaded
            //if (dataSet.Tables.Count == 0)
            //{
            //    // Assume that PersonManagement is the class which handles initial load request
            //    ApplicationTierManager.InitialLoad(dataSet);
            //}
            //else
            //{
            //    // Do different actions depending on request method
            //    switch (dataSet.Tables["RequestParameters"].Rows[0]["RequestMethhod"].ToString())
            //    {
            //        case "DataRelationTest":
            //            PersonManagement.DataRelationTest(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //            break;
            //        case "Delete":
            //            // Use different request processors depending on requestor name
            //            switch (dataSet.Tables["RequestParameters"].Rows[0]["Requestor"].ToString())
            //            {
            //                case "SilverlightDataSetTest.PersonList":
            //                    PersonManagement.Delete(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    PersonManagement.InitialLoad(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    PersonManagement.Search(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    break;
            //            }

            //            break;
            //        case "Edit":
            //            // Use different request processors depending on requestor name
            //            switch (dataSet.Tables["RequestParameters"].Rows[0]["Requestor"].ToString())
            //            {
            //                case "SilverlightDataSetTest.PersonDetails":
            //                    PersonManagement.GetPerson(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    break;
            //                case "SilverlightDataSetTest.PersonList":
            //                    PersonManagement.Edit(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    break;
            //            }
            //            break;
            //        case "GetMDS":
            //            PersonManagement.GetMDS(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //            break;
            //        case "InitialLoad":
            //            // Use different request processors depending on requestor name
            //            switch (dataSet.Tables["RequestParameters"].Rows[0]["Requestor"].ToString())
            //            {
            //                case "SilverlightDataSetTest.PersonDetails":
            //                    PersonManagement.Create(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    break;
            //                case "SilverlightDataSetTest.PersonList":
            //                    PersonManagement.InitialLoad(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    PersonManagement.Search(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    break;
            //            }

            //            break;
            //        case "LoadXmlFile":
            //            PersonManagement.LoadXmlFile(dataSet, ConfigurationManager.AppSettings["PathToXmlFile"]);
            //            break;
            //        case "MasterDetailsComboBoxDataGrid":
            //            PersonManagement.MasterDetailsComboBoxDataGrid(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //            break;
            //        case "New":
            //            // Use different request processors depending on requestor name
            //            switch (dataSet.Tables["RequestParameters"].Rows[0]["Requestor"].ToString())
            //            {
            //                case "SilverlightDataSetTest.PersonList":
            //                    PersonManagement.New(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    break;
            //            }

            //            break;
            //        case "Search":
            //            // Use different request processors depending on requestor name
            //            switch (dataSet.Tables["RequestParameters"].Rows[0]["Requestor"].ToString())
            //            {
            //                case "SilverlightDataSetTest.PersonList":
            //                    PersonManagement.Search(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    break;
            //            }

            //            break;
            //        case "Update":
            //            // Use different request processors depending on requestor name
            //            switch (dataSet.Tables["RequestParameters"].Rows[0]["Requestor"].ToString())
            //            {
            //                case "SilverlightDataSetTest.PersonDetails":
            //                    PersonManagement.Update(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    break;
            //                case "SilverlightDataSetTest.PersonList":
            //                    PersonManagement.Update(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //                    break;
            //            }

            //            break;
            //        case "UpdateMDS":
            //            PersonManagement.UpdateMDS(dataSet, ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
            //            break;
            //    }
            //}
        }
    }

    [ServiceContract(Namespace = "")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Server
    {
        /// <summary>
        /// Makes data exchange with Silverlight front-end
        /// </summary>
        /// <param name="data">Request data as xml string</param>
        /// <returns>Response data as xml string</returns>
        [OperationContract]
        public string ProcessRequest(string data)
        {
            // Validate argument
            if (string.IsNullOrEmpty(data))
            {
                // Empty request measurement
                return data;
            }

            // Restore ADO.Net DataSet from xml string
            DataSet dataSet = Connector.FromXml(data);

            // Process request from Silverlight front-end here
            ApplicationTierManager.ProcessRequest(dataSet);

            // Serialize ADO.Net DataSet into xml for sending it to Silverlight front-end
            return Connector.ToXml(dataSet, false);
        }
    }

